<template>
  <PerfectTableComponent
    :loadTableList="loadTableList"
    :changePageSize="changePageSize"
    :changePage="changePage"
    :removeQueryField="removeQueryField"
    :clearQuery="clearQuery"
    v-model:query="query"
    title="物料类别"
    ref="tableRef"
    :queryFormHeaders="queryFormHeaders"
    :table="table"
    :tableColumns="tableColumns"
    :showTableIndex="false"
    :operatorColumnWidth="300"
    :baseUrl="urls.API_URL_BACKSTAGE_MES_MATERIALTYPE"
    v-model:form="form"
    :formHeader="formHeader"
    @beforeAddRow="beforeAddRow"
    :showEditButtonInToolBar="true"
  >
  </PerfectTableComponent>
</template>

<script setup lang="ts">
import PerfectTableComponent from '@/components/base/table/PerfectTableComponent.vue';
import { onMounted, provide, reactive, ref } from 'vue';
import useTableList from '@/api/hooks/useTableList';
import type { FormHeader, ResponseData, TableColumn } from '@/types/interface/base_interface';
import urls from '@/api/request/urls';
import { getDict } from '@/api/dict';
import type { MaterialType } from '@/types/interface/mes_interface';

// 表格工具必备参数
const { table, query, loadTableList, removeQueryField, clearQuery, changePageSize, changePage } = useTableList(
  urls.API_URL_BACKSTAGE_MES_MATERIALTYPE
);
// 将表格刷新函数传到子容器
provide('loadTableList', loadTableList);

//查询==========================================================
const queryFormHeaders = ref<FormHeader[]>([]);

//表格==========================================================
// 表格插件ref对象
const tableRef = ref();

//表格列
// 定义表格所有的列字段信息
const tableColumns = ref<TableColumn[]>([]);

//表单==========================================================
//表单对象
const form = ref<MaterialType>();
//表单元素类型
const formHeader = ref<FormHeader[]>([]);

onMounted(async () => {
  //定义优先级选项。放在mounted中执行时，要将引用改变量的属性也放进来。
  const sortDict = await getDict('优先级', 'number');
  const enabledDict = await getDict('启用状态', 'boolean');
  queryFormHeaders.value = [
    // 查询表单
    {
      prop: 'name',
      label: '物料类别名称'
    },
    {
      prop: 'sort',
      label: '优先级',
      type: 'select',
      list: sortDict
    },
    {
      prop: 'enabled',
      label: '是否启用',
      type: 'select',
      list: enabledDict
    }
  ];
  //表格对象
  tableColumns.value = [
    {
      label: '名称',
      prop: 'name',
      width: 150,
      fixed: true
    },
    {
      label: '启用状态',
      prop: 'enabled',
      width: 100,
      type: 'dict',
      dict: enabledDict,
      config: {
        dictTag: {
          primary: [true],
          danger: [false]
        }
      }
    },
    {
      label: '物料数量',
      prop: 'materialNumber',
      width: 100
    },
    {
      label: '排序',
      prop: 'sort',
      width: 100,
      type: 'dict',
      dict: sortDict
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 180
    },
    {
      label: '最后编辑时间',
      prop: 'updateTime',
      width: 180
    }
  ];
  // 添加编辑表单
  formHeader.value = [
    {
      label: '名称',
      prop: 'name',
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 30,
          message: '长度在 1 到 30个字符',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '排序',
      prop: 'sort',
      type: 'select',
      list: sortDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    },
    {
      label: '是否启用',
      prop: 'enabled',
      type: 'select',
      list: enabledDict,
      rules: [
        {
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }
      ]
    }
  ];
});
//在添加行的动作以前
const beforeAddRow = async () => {
  //传入初始值
  form.value = {
    sort: 100,
    enabled: true
  };
};
</script>

<style scoped></style>
